﻿using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace json解析
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        public class All
        {
            public static string uuid;
        }

        //电信返回的base64编码后字符
        private void Button1_Click(object sender, EventArgs e)
        {
            string body = "{'deviceId':'9e1b6e12da6342e5979e2ffceeba6c7e','IMEI':'123456789234513','IMSI':'undefined','deviceType':'','tenantId':'10357554','productId':'10014764','messageType':'dataReport','topic':'v1/up/ad','assocAssetId':'','timestamp':1560389808672,'payload':{ 'APPdata':'eyJ1c2VyIjoiMTMwMTA1MTIzNDU2Nzg5LTEyMzQiLCJub2RlIjoiNzc2NjU1NDQzMzIyMTEiLCJvYmpJRCI6IiA4MTkiLCJudW0iOiIyIiwiRFQiOlt7InNlciI6IjEyNDA2IiwidGltZSI6IjA1LjE2LTEwOjQ2OjMwIiwiR0pwZXJIciI6IiAyLjUzIiwibTNwZXJIciI6IiAzMi40MjU1IiwiVGluIjoiIDc4LjU5IiwiVG91dCI6IiA1OS4zOSIsInNldGRHSiI6IiAyNDM0OS4wNCIsInN1bUdKIjoiIDIzNzc1LjExIiwic3VtTTMiOiIgMTY4MzEuODgiLCJzdW1XSHIiOiIgMTM3IiwiU1RTIjoiMDAwMCJ9LHsic2VyIjoiMTI0MDciLCJ0aW1lIjoiMDUuMTYtMTA6NDY6MzAiLCJHSnBlckhyIjoiIDIuOTEiLCJtM3BlckhyIjoiIDM1LjQ3NjEiLCJUaW4iOiIgODAuNzkiLCJUb3V0IjoiIDYwLjQ5Iiwic2V0ZEdKIjoiIDI0MzQ5LjA0Iiwic3VtR0oiOiIgMjM3NzUuMTEiLCJzdW1NMyI6IiAxNjgzMS45OSIsIlNUUyI6IjAwMDAiLCJzdW1XSHIiOiIgMTM3In1dfQ=='},'upPacketSN':'','upDataSN':'','serviceId':'','protocol':'lwm2m'}";
            JObject jsonf = (JObject)JsonConvert.DeserializeObject(body);
            string APPdata = jsonf["payload"]["APPdata"].ToString();
            textBox1.Text = APPdata.ToString();
            byte[] inArray = Convert.FromBase64String(textBox1.Text);
            textBox2.Text = System.Text.Encoding.UTF8.GetString(inArray);
        }
        //移动查看设备信息
        private void Button2_Click(object sender, EventArgs e)
        {
            string body = "{'errno':0,'error':'succ','data':{'create_time':'2019-05-13 18:51:34','online':false,'id':'526229054','observe_status':false,'title':'Lsctest01'}}";
            JObject jsonbody = (JObject)JsonConvert.DeserializeObject(body);
            string create_time = jsonbody["data"]["create_time"].ToString();
            string online = jsonbody["data"]["online"].ToString();
            string id = jsonbody["data"]["id"].ToString();
            string observe_status = jsonbody["data"]["observe_status"].ToString();
            string title = jsonbody["data"]["title"].ToString();
            textBox3.Text = create_time;
            textBox4.Text = online;
            textBox5.Text = id;
            textBox6.Text = observe_status;
            textBox7.Text = title;
        }
        //批量查看设备数据
        private void Button3_Click(object sender, EventArgs e)
        {

        }
        //写缓存命令,返回uuid
        private void Button7_Click(object sender, EventArgs e)
        {
            string body = "{'errno':0,'error':'succ','data':{'uuid':'7f38c5f6-4ee4-5997-a4a8-b851599a1a23'}}";
            JObject jsonbody = (JObject)JsonConvert.DeserializeObject(body);
            All.uuid = jsonbody["data"]["uuid"].ToString();
            textBox2.Text = All.uuid;
        }
        private void Button4_Click(object sender, EventArgs e)
        {  //字符串问题
            string body = "{'errno':0,'error':'succ','data':{'args':{'mode':1,'obj_id':3300,'trigger_msg':7,'obj_inst_id':0,'imei':'860765040384239','expired_time':'2019-06-26 12:00:00:000','valid_time':'2019-06-26 11:42:03:981','retry':3,'timeout':25},'content':[{'val':'{\'SP\':\'66\',\'RM\':[\'0030\',\'0130\',\'0230\',\'0330\',\'0430\',\'0530\',\'0630\',\'0730\',\'0830\',\'0930\',\'1030\',\'1130\',\'1230\',\'1330\',\'1430\',\'1530\',\'1630\',\'1730\',\'1830\',\'1930\',\'2030\',\'2130\',\'2230\',\'2330\']}','res_id':5750,'type':1}],'imei':'860765040384239','remain':3,'type':'WRITE','create_time':'2019-06-26 11:42:03:982','valid_time':'2019-06-26 11:42:03:981','expired_time':'2019-06-26 12:00:00:000','send_status':4,'trigger_msg':7,'feedback_msg':[{'feedback_time':'2019-06-26 12:00:02:915','feedback_status':0}]}}";
            JObject jsonbody = (JObject)JsonConvert.DeserializeObject(body);
            string mode = jsonbody["errno"].ToString();
            textBox2.Text = mode;
        }
        #region 定时器
        int pos = -1;//标志位，0停止，1开始。
        int i = 0;
        int temp = 0;//循环秒数。
        private void Button5_Click(object sender, EventArgs e)
        {
            temp = Convert.ToInt32(textBox1.Text);
            i = 0;
            pos = 1;
            textBox2.Text="开始";
        }

        private void Button6_Click(object sender, EventArgs e)
        {
            pos = 0;
            textBox2.Text="结束";
        }

        private void Timer1_Tick(object sender, EventArgs e)
        {
            if(pos==1)
            {
                i++;
                if (i == temp)
                {
                    i = 0;
                    textBox2.Text = "归零";
                }
                else
                    textBox2.Text = i.ToString();
            }
        }
        #endregion
        #region 字符串替换
        private void Button8_Click(object sender, EventArgs e)
        {
            string a = textBox1.Text;
            string b = a.Replace(":", "").Replace("+", "").Replace("-", "");
            textBox2.Text = b;
        }
        #endregion

        private void Button9_Click(object sender, EventArgs e)
        {
            //解析json中的数组
            string test = textBox1.Text;
            JArray JAtest = JArray.Parse(test);
            JObject JOtest_0 = JObject.Parse(JAtest[0].ToString());//解json数组中的对象
            JObject JOtest_1 = JObject.Parse(JAtest[1].ToString());//解json数组中的对象
            JObject JOtest_2 = JObject.Parse(JAtest[2].ToString());//解json数组中的对象
            string PLMN = JOtest_0["PLMN"].ToString();
            string TAC = JOtest_0["TAC"].ToString();
            string CID = JOtest_0["CID"].ToString();

            string RSSI = JOtest_1["RSSI"].ToString();
            string RSRQ = JOtest_1["RSRQ"].ToString();
            string RSRP = JOtest_1["RSRP"].ToString();
            string SNR = JOtest_1["SNR"].ToString();
            string TxPwr = JOtest_1["TxPwr"].ToString();

            string SlpUpT = JOtest_2["SlpUpT"].ToString();
            string Pdt = JOtest_2["Pdt"].ToString();
            string CFG = JOtest_2["CFG"].ToString();
            textBox2.Text = PLMN;
            textBox3.Text = RSSI;
            textBox4.Text = SlpUpT;
        }

        private void Button10_Click(object sender, EventArgs e)
        {

            string body = "{'data':[{'res_id':5750,'type':1,'val':{'SP':'56','RM':['0030','0130']}}]}";

            string m = "['12:00','13:00']";
            //string x= "[\"12:00\",\"13:00\"]";

            string n = m.Replace("'", "\"");
            textBox2.Text = n;
            string q = n.Replace('\\',';');
            string body2 = body.Replace('\'', '\"');

            textBox1.Text = body;

        }

        private void Button11_Click(object sender, EventArgs e)
        {
            string test = textBox1.Text;
            int numtest = test.Length;
            if (numtest == 16)
            {
                textBox2.Text = test;
            }
            else if (numtest < 16)
            {
                //重复补位到16位
                int n = 16 / numtest;
                int m = n + 1;
                StringBuilder ApTest = new StringBuilder();
                for (int i=0; i<m; i++)
                {
                    ApTest.Append(test);
                }
                string SubTest = ApTest.ToString();
                textBox2.Text = SubTest.Substring(0, 16); 
            }
            else
            {
                textBox2.Text = test.Substring(0, 16);//截取前16位
            }
        }
    }
}
